package com.epichust.bootumpp.controller;

import com.epichust.bootumpp.model.bo.ReturnBO;
import com.epichust.bootumpp.model.bo.Umpp5TaskOrderScheduleBO;
import com.epichust.bootumpp.model.entity.Umpp5TaskOrderSchedule;
import com.epichust.bootumpp.service.IUmpp5TaskOrderScheduleService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author ：longyang
 * @date ：Created in 2020/12/29
 * @description：派工单排程控制层
 */
@RestController
@RequestMapping("/api/taskOrderSchedule")
public class Umpp5TaskOrderScheduleController {

    @Autowired
    private IUmpp5TaskOrderScheduleService iUmpp5TaskOrderScheduleService;

    @RequestMapping(value = "/getAll", method = {RequestMethod.POST})
    public String getAllUmpp5TaskOrderSchedule(@RequestBody Map<String,Object> map) throws Exception{
        try{
            // 新建返回对象
            Gson gson = new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            List<Umpp5TaskOrderScheduleBO> list= iUmpp5TaskOrderScheduleService.getAllUmpp5TaskOrderSchedule(map);
            ReturnBO bo = new ReturnBO();
            bo.setState(0);
            bo.setObject(list);
            return gson.toJson(bo);
        }catch(Exception e){
            e.printStackTrace();
            Gson gson = new GsonBuilder().serializeNulls().create();
            ReturnBO bo = new ReturnBO();
            bo.setState(1);
            bo.setObject("查询失败："+e.getMessage());
            return gson.toJson(bo);
        }
    }

    /**
     * 手动排程
     * @param list
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doManualSchedule", method = {RequestMethod.POST})
    public String doManualSchedule(@RequestBody List<Umpp5TaskOrderSchedule> list) throws Exception{
        try{
            // 新建返回对象
            Gson gson = new GsonBuilder().serializeNulls().create();
            iUmpp5TaskOrderScheduleService.doManualSchedule(list);
            ReturnBO bo = new ReturnBO();
            bo.setState(0);
            bo.setObject("排程成功");
            return gson.toJson(bo);
        }catch(Exception e){
            e.printStackTrace();
            Gson gson = new GsonBuilder().serializeNulls().create();
            ReturnBO bo = new ReturnBO();
            bo.setState(1);
            bo.setObject("排程失败："+e.getMessage());
            return gson.toJson(bo);
        }
    }
}
